package com.carkeeper.bill.service.impl;

import com.carkeeper.bill.dao.BillMapper;
import com.carkeeper.bill.pojo.Bill;
import com.carkeeper.bill.service.BillService;
import com.carkeeper.subject.feign.SubjectFeign;
import com.carkeeper.subject.pojo.Store;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhouliang
 * @version 1.0
 * @date 2020/11/27 9:32
 */

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private SubjectFeign subjectFeign;

    /**
     * 条件+分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Bill> findPage(Map<String,Object> searchMap, int page, int size) {
        //设置分页参数
        PageHelper.startPage(page,size);
        //设置条件
        Example example = createExample(searchMap);
        //返回查询结果
        return (Page<Bill>)billMapper.selectByExample(example);
    }

    /**
     * 账单详情
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findBillDetails(Integer id) {
        //定义一个map
        Map<String,Object> detailsMap = new HashMap<>();
        //1.根据id查询账单详情
        Bill bill = billMapper.selectByPrimaryKey(id);
        //封装账单明细
        detailsMap.put("code",bill.getCode());
        detailsMap.put("price",bill.getPrice());
        detailsMap.put("summary",bill.getSummary());

        //2.根据商家id查询商家信息
        //获取商家id
        Long storeId = bill.getStoreId();
        Store store = subjectFeign.findById(storeId).getData();
        //封装商家详情
        detailsMap.put("name",store.getName());
        detailsMap.put("storeCode",store.getCode());
        detailsMap.put("address",store.getAddress());
        detailsMap.put("contactName",store.getContactName());
        detailsMap.put("contactPhone",store.getContactPhone());
        return detailsMap;
    }

    /*@Override
    public Page<Map<String, Object>> findBill(int page, int size) {

        return null;
    }*/


    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Bill.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 账单编号
            if(searchMap.get("code")!=null && !"".equals(searchMap.get("code"))){
                criteria.andEqualTo("code", searchMap.get("code"));
            }
            // 商家名称
            if(searchMap.get("storeName")!=null && !"".equals(searchMap.get("storeName"))){
                criteria.andLike("storeName","%"+searchMap.get("storeName")+"%");
            }
            // 支付状态
            if(searchMap.get("payState")!=null && !"".equals(searchMap.get("payState"))){
                criteria.andEqualTo("payState",searchMap.get("payState"));
            }
            // 账单日期
            if(searchMap.get("startTime")!=null && !"".equals(searchMap.get("startTime"))&& searchMap.get("endTime")!=null && !"".equals(searchMap.get("endTime"))){
                criteria.andBetween("createTime",searchMap.get("startTime"),searchMap.get("endTime"));
            }
        }
        return example;
    }
}
